﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lib_general;

namespace Lib_frmParent
{
    public partial class frmPhieunhap : Form
    {
        private DataTable dtsp;
        private DataTable dtnv;
        private DataTable dtncc;
        private DataTable dtkho;
        private Phieunhap phieunhap = new Phieunhap();
        private Function func = new Function();
        private string _smasp = "",_suserid;
        private DataRow nr;
        private decimal _did = 0;

        public frmPhieunhap()
        {
            InitializeComponent();
        }

        public string sUserid
        {
            get { return _suserid; }
            set { _suserid = value; }
        }

        private void frmPhieunhap_Load(object sender, EventArgs e)
        {
            Load_data();
            ena_object(false);

            txtngayhd.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
            txtngayct.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
            lblnguoinhap.Text = phieunhap.sHoten(_suserid);
        }

        private void Load_data()
        {
            dtsp = phieunhap.Load_form().Tables[0];
            dvghanghoa.Columns["stt"].ReadOnly = true;
            dvghanghoa.Columns["tensp"].ReadOnly = true;
            dvghanghoa.Columns["madvt"].ReadOnly = true;
            dvghanghoa.Columns["thanhtien"].ReadOnly = true;
            dvghanghoa.Columns["hide"].ReadOnly = true;

            dtnv = phieunhap.LoadCombo(cbotennv,"MANV","TEN","DMNHANVIEN");
            dtncc = phieunhap.LoadCombo(cbotenncc, "MANCC", "TEN", "DMNHACC");
            dtkho = phieunhap.LoadCombo(cbotenkho, "MAKHO", "TEN", "DMKHO");
        }

        private void butmoi_Click(object sender, EventArgs e)
        {
            ena_object(true);
            clear_text();
        }

        private void butluu_Click(object sender, EventArgs e)
        {
            if (_did == 0) _did = func.getidyymmddhhmiss_stt_computer;
            if (!phieunhap.upd_nhapth(_did,txtsohd.Text,txtngayhd.Text,decimal.Parse(txttylechietkhau.Text==""?"0":txttylechietkhau.Text),txtmancc.Text,txtngayct.Text,txtmanv.Text,0,decimal.Parse(txttongtien.Text==""?"0":txttongtien.Text),txtmakho.Text,0,_suserid))
            {
                MessageBox.Show("Không cập nhật được thông tin nhập hàng!");
                return;
            }
            for (int i = 0; i < dvghanghoa.RowCount - 1; i++)
            {
                if (!phieunhap.upd_nhapct(_did, i, dvghanghoa.Rows[i].Cells["masp"].Value.ToString(), decimal.Parse(dvghanghoa.Rows[i].Cells["soluong"].Value.ToString()), decimal.Parse(dvghanghoa.Rows[i].Cells["dongia"].Value.ToString()), decimal.Parse(dvghanghoa.Rows[i].Cells["vat"].Value.ToString()), decimal.Parse(dvghanghoa.Rows[i].Cells["thanhtien"].Value.ToString()), 0, _suserid))
                {
                    MessageBox.Show("Không cập nhật được thông tin nhập hàng!");
                    return;
                }
            }
            ena_object(false);
        }

        private void butsua_Click(object sender, EventArgs e)
        {
            ena_object(true);
        }

        private void buthuy_Click(object sender, EventArgs e)
        {

        }

        private void butboqua_Click(object sender, EventArgs e)
        {
            ena_object(false);
        }

        private void butin_Click(object sender, EventArgs e)
        {

        }

        private void butketthuc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ena_object(bool value)
        {
            butmoi.Enabled = !value;
            butluu.Enabled = value;
            butsua.Enabled = !value;
            butboqua.Enabled = value;
            butxoa.Enabled = !value;
            butketthuc.Enabled = !value;
            dvghanghoa.Enabled = value;

            txtmanv.Enabled = cbotennv.Enabled = txtngayhd.Enabled = txtngayct.Enabled = txtsohd.Enabled = value;
            txtmancc.Enabled = cbotenncc.Enabled = txtmst.Enabled = value;
            txtmakho.Enabled = cbotenkho.Enabled = txtnguoigiao.Enabled = value;
            txtnoidung.Enabled = txttylechietkhau.Enabled = value;
        }

        private void clear_text()
        {
            txtmanv.Text = cbotennv.Text = "";
            txtmancc.Text = cbotenncc.Text = txtmst.Text = "";
            txtmakho.Text = cbotenkho.Text = txtnguoigiao.Text = "";
            txtnoidung.Text = txttylechietkhau.Text = "";
            txtmanv.Focus();
        }

        private void txtmanv_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) SendKeys.Send("{Tab}{F4}");
        }

        private void txtnoidung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) dvghanghoa.Focus();
        }

        private void cbotennv_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) txtsohd.Focus();
        }

        private void cbotenncc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) txtmst.Focus();
        }

        private void cbotenkho_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) txtnguoigiao.Focus();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            try
            {
                const int WM_KEYDOWN = 0x100;
                const int WM_SYSKEYDOWN = 0x104;

                if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
                {
                    if (ActiveControl.Name == "dvghanghoa")
                    {
                        if (keyData == Keys.Enter)
                        {
                            SendKeys.SendWait("{right}");
                            if (dvghanghoa.CurrentCell.ColumnIndex == dvghanghoa.ColumnCount - 1)
                            {
                                dvghanghoa.AllowUserToAddRows = true;
                                dvghanghoa.CurrentCell = dvghanghoa[0, dvghanghoa.CurrentRow.Index + 1];
                            }
                            return true;
                        }
                    }
                }
            }
            catch { }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void dvghanghoa_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                //timkiem(dataGridView1.CurrentCell.Value.ToString());
                dvghanghoa.AllowUserToAddRows = false;
                Message a = new Message();
                a.Msg = 256;
                ProcessCmdKey(ref a, Keys.Enter);

                int i = dvghanghoa.CurrentCell.ColumnIndex;
                if (i == dvghanghoa.ColumnCount - 1)
                {
                    if (dvghanghoa.CurrentCell.Value != null)
                    {
                        dvghanghoa.AllowUserToAddRows = true;
                        dvghanghoa.CurrentCell = dvghanghoa[0, dvghanghoa.CurrentRow.Index + 1];
                    }
                }
            }
            catch { }
        }

        private void dvghanghoa_CurrentCellChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.ActiveControl == dvghanghoa)
                {
                    DataRow dr = func.getrowbyid(dtsp, "masp='" + dvghanghoa.CurrentRow.Cells[1].Value + "'");
                    if (dvghanghoa.CurrentRow.Cells[1].Value == null || dr == null)
                    {
                        frmTimkiem f = new frmTimkiem();
                        f.ShowDialog();
                        this._smasp = f._smasp;
                        if (this._smasp != "")
                        {
                            nr = func.getrowbyid(dtsp, "masp='" + this._smasp + "'");
                            dvghanghoa.CurrentRow.Cells[0].Value = dvghanghoa.RowCount;
                            dvghanghoa.CurrentRow.Cells[1].Value = this._smasp;
                            dvghanghoa.CurrentRow.Cells[2].Value = nr["tensp"].ToString();
                            dvghanghoa.CurrentRow.Cells[3].Value = nr["tendvt"].ToString();
                        }
                        else
                        {
                            //dvghanghoa.Rows.Remove(dvghanghoa.CurrentRow);
                            txttylechietkhau.Focus();
                        }
                    }                    
                }
            }
            catch { }
        }

        private void dvghanghoa_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dvghanghoa.CurrentCell.ColumnIndex == 5 && dvghanghoa.CurrentRow.Cells[5].Value != null)
                {
                    decimal _dsoluong = (dvghanghoa.CurrentRow.Cells[4].Value == null ? 0 : decimal.Parse(dvghanghoa.CurrentRow.Cells[4].Value.ToString()));
                    decimal _ddongia = (dvghanghoa.CurrentRow.Cells[5].Value == null ? 0 : decimal.Parse(dvghanghoa.CurrentRow.Cells[5].Value.ToString()));
                    decimal _dthanhtien = (_dsoluong * _ddongia);
                    dvghanghoa.CurrentRow.Cells[7].Value = _dthanhtien;
                }
            }
            catch { }
        }

        private void cbotennv_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == cbotennv)
            {
                try
                {
                    txtmanv.Text = cbotennv.SelectedValue.ToString();
                }
                catch { }
            }
        }

        private void txtmanv_Validated(object sender, EventArgs e)
        {
            if (cbotennv.Text != "")
                cbotennv.SelectedValue = txtmanv.Text;
        }
    }
}
